package com.wjj.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wjj.entity.Groups;
import com.wjj.entity.Pay;
import com.wjj.entity.User;
import com.wjj.service.PayService;

@Controller
public class PayController {
	@Autowired
	private PayService payService;
	//展示列表
	@RequestMapping("/")
	public String selectPay(Model model,@RequestParam(defaultValue = "1")Integer pageNum,Pay pay) {
		PageInfo<Pay> pg = payService.selectPay(pageNum,pay);
		List<Groups> list = payService.getCompany();
		model.addAttribute("list", list);
		System.err.println(pay);
		model.addAttribute("pay", pay);
		model.addAttribute("pg", pg);
		return "list";
	}
	//跳转到添加页面
	@RequestMapping("toAdd")
	public String toAdd(Model model) {
		List<Groups> list = payService.getCompany();
		model.addAttribute("p", new Pay());
		model.addAttribute("list", list);
		return "add";
	}
	//添加
	@RequestMapping("addPay")
	public String addPay(@ModelAttribute("p")Pay pay,MultipartFile file) throws IllegalStateException, IOException {
		// TODO Auto-generated method stub
		if(file!=null&&file.getSize()>0) {
			String originalFilename = file.getOriginalFilename();
			String fileName = UUID.randomUUID().toString().replaceAll("-", "")+originalFilename;
			File file2 = new File("D:/phone",fileName);
			file.transferTo(file2);
			pay.setBills("/phone/"+fileName);
		}
		System.out.println("添加的pay"+pay);
		//添加pay
		payService.addPay(pay);
		return "redirect:/";
	}
	//三级联动
	@RequestMapping("getGroups")
	@ResponseBody
	public List<Groups> getGroups(Integer pid) {
		List<Groups> list = payService.getGroups(pid);
		return list;
	}
	//跳转到修改图页面
	@RequestMapping("toUpdate")
	public String toUpdate(Model model,Integer id) {
		//查找所有的公司
		List<Groups> list = payService.getCompany();
		//根据id查找对象
		Pay pay = payService.getPay(id);
		//创建对象
		model.addAttribute("p", new Pay());
		//回显对象
		model.addAttribute("pay", pay);
		//输出我回显的对象
		System.err.println(pay);
		//查找发薪人
		List<User> user =  payService.listtUser(id);
		//将发信人存放在作用域李
		model.addAttribute("user", user);
		//三级联动公司的联动
		model.addAttribute("list", list);
		return "update";
	}
	//修改
	@RequestMapping("update")
	public String updatePay(Pay pay,MultipartFile file) throws IllegalStateException, IOException {
		// TODO Auto-generated method stub
		if(file!=null&&file.getSize()>0) {
			String originalFilename = file.getOriginalFilename();
			String fileName = UUID.randomUUID().toString().replaceAll("-", "")+originalFilename;
			File file2 = new File("D:/phone",fileName);
			file.transferTo(file2);
			pay.setBills("/phone/"+fileName);
		}
		System.out.println("修改的pay"+pay);
		//找到你要修改的id
		Integer id = pay.getId();
		System.err.println("我要修改的id"+id);
		//修改pay
		payService.updatePay(pay);
		return "redirect:/";
	}
}	
